﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: NamedValuedAnnotatableGenomicSegment.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2013-01-25 3:25 PM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using BOL.Models.Genome;

namespace BOL.Models.Features
{
    public class NamedValuedAnnotatableGenomicSegment<TValue> : ValuedGenomicSegment<TValue>, INamed, IAnnotatable
        where TValue: struct
    {
        public string Name { get; set; }

        public IDictionary<string, dynamic> Annotations { get; private set; }

        public NamedValuedAnnotatableGenomicSegment(Chromosome chromosome, int from, int to, string name, TValue value, Direction direction = Direction.Forward)
            : base(chromosome, from, to, value, direction)
        {
            Name = name;
            Annotations = new Dictionary<string, dynamic>();
        }

        public string ToBedString(bool itemRgb)
        {
            var sb = new StringBuilder();
            sb.AppendFormat("{0}\t{1}\t{2}", Chromosome, From, To);
            if (Name == null)
                return sb.ToString();
            sb.AppendFormat("\t{0}", Name);
            if (!Value.HasValue)
                return sb.ToString();
            sb.AppendFormat("\t{0}", Value.Value);
            switch (Direction)
            {
                case Direction.Forward: sb.Append("\t+"); break;
                case Direction.Backward: sb.Append("\t-"); break;
                default: sb.Append("\t"); break;
            }
            if (!Annotations.ContainsKey("thickStart"))
                return sb.ToString();

            sb.AppendFormat("\t{0}\t{1}", Annotations["thickStart"], Annotations["thickEnd"]);
            if (itemRgb)
                sb.AppendFormat("");
            if (!Annotations.ContainsKey("thickEnd"))
                sb.Append("\t");


            return String.Format("{0}\t{1}\t{2}\t{3}\t", Chromosome, From, To, Value);
        }

        public override string ToString()
        {
            return String.Format("{0}:{1}-{2}({3}:{4})", Chromosome, From, To, Name, Value);
        }
    }
}
